#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct test
{
	int data;
	struct test *next;
}*linklist,node;

void display(linklist L)
{
	node *p;
	p=L->next;
	int count=0;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		count++;
		if(count%10==0)
			cout<<endl;
		p=p->next;
	}
	cout<<endl;
}

linklist create(linklist L)
{
	node *p,*tail;
	int num;
	L=(linklist)malloc(sizeof(node));
	L->next=NULL;
	cout<<"请输入一个数：";
	cin>>num;
	srand(time(NULL));
	while(num>0)
	{
		p=(linklist)malloc(sizeof(node));
		p->data=(rand()%1000)*9+1000;
		if(L->next!=NULL)
		{
			tail->next=p;
			tail=p;
		}
		else
		{
			L->next=p;
			tail=p;
		}
		tail->next=NULL;
		num--;
	}
	return L;
}

void sort(linklist L)
{
	node *p,*q,*ptl,*pre;
	p=L->next;
	while(p!=NULL)
	{
		q=L;
		pre=q->next;
		ptl=pre->next;
		while(ptl!=NULL)
		{
			if(ptl->data < pre->data)
			{
				pre->next=ptl->next;
				ptl->next=q->next;
				q->next=ptl;
				pre=q->next;
				ptl=pre->next;
			}
			else
			{
				q=q->next;
				pre=q->next;
				ptl=pre->next;
			}
		}
		p=p->next;
	}
}

int main()
{
	linklist Q;
	cout<<"create list:"<<endl;
	Q=create(Q);
	display(Q);
	cout<<"after sort:"<<endl;
	sort(Q);
	display(Q);
	return 0;
}
